-- tree-sitter 配置
-- 相关配置可参考：https://gitee.com/zgpio/nvim-treesitter

local inori_config = require("inori.config").inori_config
local proxy = require("inori.proxy")
local utils = require("inori.utils")
local github_proxy_prefix = proxy.get_github_proxy_prefix() .. "https://github.com/"

local parsers = require("nvim-treesitter.parsers")
-- 设置拉去解析器的url镜像
local set_parsers_mirror = function()
  for parser_name, config in pairs(parsers) do
    if config and config.install_info and config.install_info.url then
      local old = config.install_info.url
      if not old:match("^" .. github_proxy_prefix) then
        local replace_url = config.install_info.url:gsub("^https?://github.com/", github_proxy_prefix)
        config.install_info.url = replace_url
        if inori_config.debug_mode then
          utils.print_msg({ "[debug]", "parser name: ", parser_name, "tree-sitter url: ", config.install_info.url })
        end
      end
    end
  end
end

local init_function = function(plugin)
  -- 新版自定义各个Parser配置方式
  vim.api.nvim_create_autocmd("User", {
    pattern = "TSUpdate",
    callback = function()
      set_parsers_mirror()
    end,
  })
  set_parsers_mirror()
end

return {
  "nvim-treesitter/nvim-treesitter",
  init = init_function,
  opts = {
    -- Automatically install missing parsers when entering buffer
    -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
    auto_install = false,
  },
}
